package com.yunxi.yunxicaiproject.tool;


import com.yunxi.yunxicaiproject.entity.po.*;
import com.yunxi.yunxicaiproject.entity.vo.ActivityParticipationVO;
import com.yunxi.yunxicaiproject.entity.vo.LeaveApplicationVO;
import com.yunxi.yunxicaiproject.entity.vo.ScheduleVO;
import com.yunxi.yunxicaiproject.mapper.*;
import com.yunxi.yunxicaiproject.service.*;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Component
public class StudentTools {

    @Resource
    private final AiStudentMapper studentMapper;
    @Resource
    private final AiActivityMapper activityMapper;
    @Resource
    private IAiStudentService studentService;
    @Resource
    private IAiClassService classService;
    @Resource
    private IAiScheduleService scheduleService;
    @Resource
    private AiTeacherMapper teacherMapper;
    @Resource
    private AiCourseMapper courseMapper;
    @Resource
    private IAiActivityService aiActivityService;
    @Resource
    private IAiLeaveApplicationService leaveApplicationService;
    @Resource
    private IAiActivityParticipationService aiActivityParticipationService;
    @Resource
    private IAiActivityService activityService;
    @Resource
    private AiLeaveApplicationMapper leaveApplicationMapper;

    @Tool(description = "根据条件查询学生信息")
    public List<AiStudent> getStudentList(@ToolParam(description = "需要查询的条件") AiStudent student) {

        if (student == null) {
            return studentMapper.selectList(null);
        }
        List<AiStudent> list = studentService.query()
                .select("student_id", "name", "phone", "gender", "class_id", "status")
                .eq(student.getName() != null, "name", student.getName())
                .eq(student.getPhone() != null, "phone", student.getPhone())
                .eq(student.getStudentId() != null, "student_id", student.getStudentId())
                .eq(student.getGender() != null, "gender", student.getGender())
                .eq(student.getClassId() != null, "class_id", student.getClassId())
                .list();
        return list;
    }

    @Tool(description = "学号查班级")
    public List<AiClass> getClassName(@ToolParam(description = "学生学号") String studentId) {
        AiStudent student = studentMapper.selectById(studentId);
        if (student == null) {
            return null;
        }
        List<AiClass> classList = classService.query()
                .eq("class_id", student.getClassId())
                .list();
        return classList;
    }

    @Tool(description = "学号查询课程信息")
    public List<ScheduleVO> getSchedule(@ToolParam(description = "学生学号") String studentId) {
        AiStudent student = studentMapper.selectById(studentId);
        if (student == null) {
            return null;
        }
        List<AiSchedule> schedules = scheduleService.query()
                .eq("class_id", student.getClassId())
                .list();

        List<ScheduleVO> list = new ArrayList<>();
        for (AiSchedule schedule : schedules) {
            ScheduleVO scheduleVO = new ScheduleVO();
            scheduleVO.setScheduleId(schedule.getScheduleId());
            scheduleVO.setClassId(schedule.getClassId());
            scheduleVO.setLocation(schedule.getLocation());
            scheduleVO.setEndTime(schedule.getEndTime());
            scheduleVO.setStartTime(schedule.getStartTime());
            scheduleVO.setDayOfWeek(schedule.getDayOfWeek());
            scheduleVO.setTeacherName(teacherMapper.selectById(schedule.getTeacherId()).getName());
            scheduleVO.setCourseName(courseMapper.selectById(schedule.getCourseId()).getCourseName());
            list.add(scheduleVO);
        }

        return list;
    }

    @Tool(description = "查询所有活动信息")
    public List<AiActivity> getActivityList() {
        return aiActivityService.list();
    }

    @Tool(description = "学号查询参与的活动信息")
    public List<ActivityParticipationVO> getActivityParticipationInfo(@ToolParam(description = "学生学号") String studentId) {
        List<AiActivityParticipation> leaveApplications = aiActivityParticipationService.query()
                .eq("student_id", studentId)
                .list();
        String name = studentMapper.selectById(studentId).getName();
        List<ActivityParticipationVO> collect = leaveApplications.stream().map(item -> {
            ActivityParticipationVO vo = new ActivityParticipationVO();
            BeanUtils.copyProperties(item, vo);
            vo.setStudentName(name);
            String title = activityMapper.selectById(item.getActivityId()).getTitle();
            vo.setTitle(title);
            return vo;
        }).toList();
        return collect;
    }

    @Tool(description = "学号查询请假信息")
    public List<LeaveApplicationVO> getLeaveInfo(String studentId) {
        List<AiLeaveApplication> leaveApplications = leaveApplicationService.query()
                .eq("student_id", studentId)
                .list();
        String name = studentMapper.selectById(studentId).getName();
        List<LeaveApplicationVO> list = new ArrayList<>();
        for (AiLeaveApplication leaveApplication : leaveApplications) {
            LeaveApplicationVO leaveApplicationVO = new LeaveApplicationVO();
            leaveApplicationVO.setStudentId(leaveApplication.getStudentId());
            leaveApplicationVO.setLeaveId(leaveApplication.getLeaveId());
            leaveApplicationVO.setReason(leaveApplication.getReason());
            leaveApplicationVO.setStatus(leaveApplication.getStatus());
            leaveApplicationVO.setStartTime(leaveApplication.getStartTime());
            leaveApplicationVO.setEndTime(leaveApplication.getEndTime());
            leaveApplicationVO.setType(leaveApplication.getType());
            leaveApplicationVO.setStudentName(name);
            leaveApplicationVO.setScheduleName(courseMapper.selectById(leaveApplication.getScheduleId()).getCourseName());
            list.add(leaveApplicationVO);
        }
        return list;
    }

    @Tool(description = "请假申请，返回请假信息")
    public AiLeaveApplication createLeaveInfo(
            @ToolParam(description = "学号") String studentId,
            @ToolParam(description = "课程号") Integer scheduleId,
            @ToolParam(description = "请假开始时间") LocalDateTime startTime,
            @ToolParam(description = "请假结束时间") LocalDateTime endTime,
            @ToolParam(description = "请假类型") String type,
            @ToolParam(description = "请假原因") String reason) {
        AiLeaveApplication leaveApplication = new AiLeaveApplication();
        leaveApplication.setStudentId(studentId);
        leaveApplication.setType(type);
        leaveApplication.setReason(reason);
        leaveApplication.setStartTime(startTime);
        leaveApplication.setEndTime(endTime);
        leaveApplication.setScheduleId(scheduleId);
        leaveApplication.setStatus(0);
        leaveApplicationService.save(leaveApplication);

        return leaveApplication;
    }

    @Tool(description = "参加活动，返回参加成功或者你已经参加了")
    public String createActivityParticipation(
            @ToolParam(description = "学号") String studentId,
            @ToolParam(description = "活动号") Integer activityId) {
        AiActivityParticipation participation = new AiActivityParticipation();


        List<AiActivityParticipation> list = aiActivityParticipationService.query()
                .eq("student_id", studentId)
                .eq("activity_id", activityId)
                .list();
        if (list.isEmpty()) {
            participation.setStudentId(studentId);
            participation.setActivityId(activityId);
            participation.setStatus(1);
            aiActivityParticipationService.save(participation);
            return "参加成功";
        }
        return "已经参加活动";
    }

}
